<%@page import="java.sql.ResultSet"%>
<%@page import="ContentManagers.RoleManagers.PMManager"%>
<div id ="cart_body" class="content_box">
    
    <div id ="cart_contents">
                            
    <% try{
    
        //initialize values
        int cartDepth = 0;
        int cartHeight, test = 0;
        int totalAmt = 0;
        ResultSet rs;
        Object cartObject = session.getAttribute("cartArray");
        String[][] userCart = (String[][])cartObject;
        cartHeight = userCart[0].length;
        
        try{    if(session.getAttribute("errorMessage") == "cF")
                       {    //array full
                            out.print("Your cart is full, item not added.<br>");
                            session.removeAttribute("errorMessage");
                        }
            
            }catch(Exception e){}
        
        for(test = 0; test < cartHeight; test++) //get cartDepth
            if(userCart[0][test] != null && Integer.parseInt(userCart[1][test]) != -1 && Integer.parseInt(userCart[0][test]) != -1)
              { cartDepth++;
                
                rs = new PMManager().getProduct(userCart[0][test]);     
                
                if(rs == null ||cartDepth == 0)
                { 
                    out.println("Invalid Input");
                }
                
                else
                        {   //display items in cart
                            while(rs.next())
                            {
                                %> 
                                <div class="searchItem_result">
                                    <table>
                                        <tr>
                                            <td>
                                                <div class="searchItem_result_image">
                                                    <img src="User Interface/images/default_item_image.png">
                                                </div>
                                            </td>
                                            <td>
                                                <div class="searchItem_result_info">
                                                    <div class="editCartForm">
                                                        <form name ="editCart_<% out.println(test); %>" method="post" onsubmit="return addToCartCheck(this)" action="ServletEditCart">
                                                            Edit Qty:&nbsp;<input style="width:25px; height:13px;"type="text" value="<% out.println(userCart[1][test]); %>" name="quantity" maxlength="3" autocomplete="off"/> 
                                                                <input type="text" class="invisible" name="cartIndex" value="<% out.println(test); %>"/>
                                                                <input type="submit" name="submitEdit" value="Edit"/><input type="submit" name="submitEdit" value="Delete"/>
                                                        </form>
                                                    </div>
                                                     
                                                        <div class="searchItem_result_title"><a href="viewItem.jsp?productID=<% out.println(rs.getString("product_id")); %> ">
                                                        <%  out.println(rs.getString("product_name")); %></a></div>
                                                        <div class="searchItem_result_info_stuff">
                                                        <%  
                                                            out.println("<br><Table><tr><td>Price:</td><Td> Php.&nbsp; <B> " + rs.getString("price") +"</b> </td></tr>");
                                                            out.println("<tr><td>Sub-total: </td><td> Php.&nbsp; <b> " + Integer.parseInt(rs.getString("price"))*Integer.parseInt(userCart[1][test]) + "</b> </td></tr></table>");
                                                            totalAmt = totalAmt + Integer.parseInt(rs.getString("price"))*Integer.parseInt(userCart[1][test]);
    %>
                                                        </div>
                                                    
                                                </div>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                
                                <%

                            }
                        }
               }
               if(cartDepth > 0){
               %> 
                    </div>
                    <div id="cart_summary">
                        <table>
                            <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;Items in cart: </td> <tD> <% out.println(cartDepth+1); %> </Td></tr>
                            <Tr><Td>&nbsp;&nbsp;&nbsp;&nbsp;Total amount: </td> <td> Php.<% out.println(totalAmt); %> </td></tr>
                            <tr><td></td><td></td></tr>
                        </table>
                    </div>
                            <br><br><br><br><br>&nbsp;&nbsp;&nbsp;
                            <a href="#" onclick="showReAuth();">Purchase Cart Items</a>
                            
                            <div id="reAuthCustomer">
                                <form name = "reAuthenticateCustomer" method="post" onsubmit="" action="ServletPurchaseItems">
                                &nbsp;&nbsp;&nbsp;
                                <input type="text" name="reAuthPassword" value="Password" onfocus="clearReAuthPassword(this, 'Password')"/>
                                <input type="submit" id="submitReAuth" name="submitReAuth" value="Continue" disabled/>
                                </form>
                                
                            </div>
                    <%
               }
               else
               { out.println("</div>No items in cart yet. Start browsing now!");
               }
                                     
        
    
    }catch(Exception e){ out.println("</div>No products in cart yet. Start browsing now!");  }   

    %>
    
    
</div>
